<template>
  <div>
    <van-nav-bar title="账号登录" left-arrow @click-left="onClickLeft" />
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="username"
        placeholder="请输入账号"
        :rules="[
          { required: true, message: '请填写用户名' },
          {
            pattern: /^\w{5,8}$/,
            message: '用户名格式5-8位的字符和数字'
          }
        ]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        placeholder="请输入密码"
        :rules="[
          { required: true, message: '请填写密码' },
          {
            pattern: /^\w{5,12}$/,
            message: '密码格式5-12位的字符和数字'
          }
        ]"
      />
      <div style="margin: 16px">
        <van-button block type="info" native-type="submit">登录 </van-button>
      </div>
    </van-form>
    <router-link class="link" to="/register">还没有账号，去注册～</router-link>
  </div>
</template>

<script>
import { login } from '@/api/user'
import { setToken } from '@/utils/storage'
export default {
  name: 'login-page',
  data () {
    return {
      username: '',
      password: ''
    }
  },
  methods: {
    async onSubmit (values) {
      // console.log('submit', values)
      const data = await login(values)
      setToken(data.token)
      this.$toast.success('登录成功')
      this.$router.push('/userDetails')
    },
    onClickLeft () {
      this.$router.go(-1)
    }
  }
}
</script>

<style lang="less" scoped>
.link {
  color: #333;
  font-size: 13px;
  padding-left: 5%;
}
</style>
